﻿@using Resources;
@using HP.HPTRIM.Web.Configuration;
@using HP.HPTRIM.ServiceModel;
@using System.Linq;

@functions {

    public bool isInThumbnailMode()
    {
        ListItemDisplayType listType = WebDrawerConfiguration.Instance.UISettings.ListItemDisplayType;
        
        if (this.TrimHelper.TrimType == BaseObjectTypes.Record)
        {
            if (listType == ListItemDisplayType.ThumbnailOnly)
            {
                return true;
            }
            

            if (this.Request.QueryString["thumbnails"] != null)
            {
                if (this.Request.QueryString["thumbnails"] == "true")
                {
                    return listType == ListItemDisplayType.ThumbnailAndChoose
                        || listType == ListItemDisplayType.TextAndChoose;
                        
                }
            }
            else
            {
                return listType == ListItemDisplayType.ThumbnailAndChoose;
            }
        }
        return false;
    }
    
}

@if (Model != null && Model.Results != null && Model.Results.Count > 0)
{

    bool isThumbnails = isInThumbnailMode();


    int progressiveCount = 0;

    Int32.TryParse(HttpContext.Current.Request.QueryString["start"], out progressiveCount);

    if (progressiveCount > 0)
    {
        progressiveCount = progressiveCount - 1;
    }


    progressiveCount = progressiveCount + Model.Results.Count;

    int pagingLength = 4;
    int pagingHalfWay = (int)Math.Ceiling((double)pagingLength / 2); ;

    int pageSize = 0;

    if (Int32.TryParse(HttpContext.Current.Request.QueryString["pageSize"], out pageSize) == false)
    {
        pageSize = ServiceDefaults.Instance.Searching.PageSize;
    }

    long pageNo = (int)Math.Ceiling((double)progressiveCount / pageSize);
    long totalPages = (Model.TotalResults + pageSize - 1) / pageSize;
    List<long> pages = new List<long>();

    int counter = 1;
    while (pages.Count < pagingLength && pages.Count < totalPages)
    {
        if (pageNo > pagingHalfWay && pageNo <= (totalPages - pagingHalfWay))
        {
            pages.Add(counter + pageNo - pagingHalfWay);
        }
        else if (pageNo > pagingHalfWay)
        {
            pages.Add((totalPages - pagingLength) + counter);
        }
        else
        {
            pages.Add(counter);
        }
        counter++;
    }

    @Html.Partial(isThumbnails ? "resultsThumbnails" : "resultsList", new { Results = Model.Results, PropertiesAndFields = Model.PropertiesAndFields })



    if (this.Model.TotalResults > 0)
    {
        <div class="pagination pagination-centered">
            <ul id="trim-pagination">
                @{
        var queryString = new System.Collections.Specialized.NameValueCollection(HttpContext.Current.Request.QueryString);
        if ((progressiveCount - 1) > Model.Results.Count)
        {
            queryString["pageSize"] = pageSize.ToString();
            queryString["start"] = "1";
            <li>
                <a href="?@queryString.ToFormUrlEncoded()">&laquo;</a>
            </li>
            queryString["start"] = (progressiveCount - (pageSize * 2)).ToString();
            <li>
                <a href="?@queryString.ToFormUrlEncoded()">&lt;</a>
            </li>

        }
        else
        {
            <li class="disabled"><a href="#">&laquo;</a></li>
            <li class="disabled"><a href="#">&lt;</a></li>
        }

        foreach (long pageInSet in pages)
        {
            queryString["pageSize"] = pageSize.ToString();
            queryString["start"] = (((pageInSet - 1) * pageSize) + 1).ToString();

            <li class="@(pageInSet == ((pageNo > 1) ? pageNo : pageNo) ? "active" : "")">
                <a href="?@queryString.ToFormUrlEncoded()">@pageInSet</a>
            </li>
        }

        if (progressiveCount < Model.TotalResults)
        {
            queryString["pageSize"] = pageSize.ToString();
            queryString["start"] = (progressiveCount == pageSize ? progressiveCount + 1 : progressiveCount).ToString();
            <li>
                <a href="?@queryString.ToFormUrlEncoded()">></a>
            </li>
            queryString["start"] = (totalPages * (pageSize-1)).ToString();
            <li>
                <a href="?@queryString.ToFormUrlEncoded()">&raquo;</a>
            </li>
        }
        else
        {
            <li class="disabled"><a href="#">></a></li>
    <li class="disabled"><a href="#">&raquo;</a></li>
        }
}
            </ul>
        </div>
    }
    else
    {
        int start = 0;
        Int32.TryParse(HttpContext.Current.Request.QueryString["start"], out start);

        var queryString = new System.Collections.Specialized.NameValueCollection(HttpContext.Current.Request.QueryString);

        <div class="pagination pagination-centered">
            <ul id="trim-pagination">
                <li class="@(start < 2 ? "disabled" : null)">
                    @{
        queryString["start"] = (start > pageSize ? start - pageSize : 0).ToString();
        <a href="?@queryString.ToFormUrlEncoded()">&lt;</a>
                    }
                </li>

                <li class="@(!this.Model.HasMoreItems ? "disabled" : null)">
                    @if (this.Model.HasMoreItems)
                    {
                        start = (start == 0 ? start + 1 : start);
                        queryString["start"] = (start + pageSize).ToString();
                        <a href="?@queryString.ToFormUrlEncoded()">></a>
                    }
                    else
                    {
                        <span>&raquo;</span>
                    }
                </li>
            </ul>
        </div>
    }
}
else
{

    <div class="alert alert-block">
        @if (this.Model.ResponseStatus != null && this.Model.ResponseStatus.Message != null)
        {
            <h4>@this.Model.ResponseStatus.Message</h4>

        }
        else
        {
            <h3>@Model.SearchTitle</h3>
            @Translations.lang.warning_no_matching
        }

    </div>
}

